<?php

abstract class Ext_Model_DbTable_Abstract extends Zend_Db_Table_Abstract
{
    /**
     * @param int $id
     * @return Zend_Db_Table_Row
     */
    public function findById($id)
    {
        return $this->find($id)->current();
    }

    /**
     * @param array $filters
     * @return Zend_Db_Table_Select
     */
    public function getListQuery($filters = array())
    {
        return $this->select();
    }

    /**
     * @param array $filters
     * @return Zend_Paginator_Adapter_DbTableSelect 
     */
    public function fetchPaginatorAdapter($filters = array())
    {
        $query = $this->getListQuery($filters);
        return new Zend_Paginator_Adapter_DbTableSelect($query);
    }

    public function __call($function, $args)
    {
        if (strpos($function, 'findBy') === 0) {
            $criteria = strtolower(substr($function, 6));
            return $this->fetchAll(
                $this->select()->from($this->_name)->where($criteria . ' = ?', $args)
            );
        } else if (strpos($function, 'findOneBy') === 0) {
            $criteria = strtolower(substr($function, 9));
            return $this->fetchRow(
                $this->select()->from($this->_name)->where($criteria . ' = ?', $args)
            );
        } else {
            return false;
        }
    }

}